package com.deer.wms.base.system.web.bill;

import com.deer.wms.base.system.model.bill.*;
import com.deer.wms.base.system.model.box.BoxInfo;
import com.deer.wms.base.system.model.box.BoxItem;
import com.deer.wms.base.system.model.box.BoxItemDto;
import com.deer.wms.base.system.model.task.PickTaskDto;
import com.deer.wms.base.system.model.task.TaskInfo;
import com.deer.wms.base.system.model.task.TaskInfoCriteria;
import com.deer.wms.base.system.model.task.TaskInfoDto;
import com.deer.wms.base.system.model.ware.CellInfo;
import com.deer.wms.base.system.service.bill.BillOutWorkService;
import com.deer.wms.base.system.service.bill.IBillOutDetailService;
import com.deer.wms.base.system.service.bill.IBillOutMasterService;
import com.deer.wms.base.system.service.box.BoxInfoService;
import com.deer.wms.base.system.service.box.BoxItemRecordService;
import com.deer.wms.base.system.service.box.IBoxItemService;
import com.deer.wms.base.system.service.task.ITaskInfoService;
import com.deer.wms.base.system.service.task.PickTaskService;
import com.deer.wms.base.system.service.task.TaskInfoHistoryService;
import com.deer.wms.base.system.service.ware.ICellInfoService;
import com.deer.wms.common.core.controller.BaseController;
import com.deer.wms.common.core.domain.AjaxResult;
import com.deer.wms.common.core.page.TableDataInfo;
import com.deer.wms.common.core.result.Result;
import com.deer.wms.common.core.result.ResultGenerator;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/***
 *出库作业
 * @author 李帅辰
 * @date 2020/9/3 10:15
 * @return 
 */
@Controller
    @RequestMapping("/out/billoutwork")
public class BillOutWorkController extends BaseController {
    @Autowired
    private BillOutWorkService billOutWorkService;
    @Autowired
    private ITaskInfoService taskInfoService;
    @Autowired
    private BoxItemRecordService boxItemRecordService;
    @Autowired
    private IBoxItemService boxItemService;
    @Autowired
    private BoxInfoService boxInfoService;
    @Autowired
    private ICellInfoService cellInfoService;
    @Autowired
    private IBillOutDetailService billOutDetailService;
    @Autowired
    private PickTaskService pickTaskService;
    @Autowired
    private TaskInfoHistoryService taskInfoHistoryService;

    @Autowired
    private IBillOutMasterService billOutMasterService;
    private String prefix = "out/billOutWork";

    /**
     * 跳转页面
     * @return
     */
    @RequiresPermissions("out:billOutWork:view")
    @GetMapping("/page")
    public String workMaster()
    {
        return prefix + "/billOutWork";
    }


    /**
     * 跳转页面
     * @return
     */
    @RequiresPermissions("out:assignBillOutWork:view")
    @GetMapping("/assign")
    public String assign()
    {
        return prefix + "/assign";
    }


    /**
     * 主页面显示
     * @param billOutMasterCriteria
     * @return
     */
    @RequiresPermissions("out:billOutWork:list")
    @PostMapping("/findList")
    @ResponseBody
    @Transactional
    public TableDataInfo findList(BillOutMasterCriteria billOutMasterCriteria) {
        startPage();
        List<BillOutMasterDto> list = billOutWorkService.findList(billOutMasterCriteria);
        return getDataTable(list);
    }

    /**
     * 出库作业 根据billId查询出库单信息
     */
    @PostMapping("/findBillOutMasterDtoByBillId")
    @ResponseBody
    public BillOutMasterDto findBillOutMasterDtoByBillId(Integer billId){
         BillOutMasterDto billOutMasterDto= billOutWorkService.findBillOutMasterDtoByBillId(billId);
        return billOutMasterDto;
    }


    /**
     * 出库作业，出库物料明细
     * @param billOutDetailDto
     * @return
     */
    @PostMapping("/selectListByBillId")
    @ResponseBody
    public TableDataInfo selectListByBillId(BillOutDetailDto billOutDetailDto) {
        startPage();
        List<BillOutDetailDto> list = billOutWorkService.selectListByBillId(billOutDetailDto);
        return getDataTable(list);
    }
    /***
     *查询某一条
     * @author 李帅辰
     * @date 2020/10/16 14:11 
     * @return com.deer.wms.base.system.model.bill.BillOutDetailDto
     */
    @PostMapping("/selectEditQuantity")
    @ResponseBody
    public  BillOutDetailDto selectEditQuantity (BillOutDetailDto billOutDetailDto){
        BillOutDetailDto billOutDetailDto1 = billOutWorkService.selectEditQuantity(billOutDetailDto);
        return billOutDetailDto1;
    }
    /**
     * 详情
     */
    @GetMapping("/detail")
    public String detail() {
        return prefix + "/detail";
    }

    /**
     * 在出库单作业列表显示任务列表
     * @param billId
     * @return
     */
    @ResponseBody
    @RequestMapping("/getTaskinfoByBillId")
    public TableDataInfo selectTaskinfoByBillId(Integer billId) {
        startPage();
        List<TaskInfo> taskInfos = billOutWorkService.selectTaskInfoBybillId(billId);
        return getDataTable(taskInfos);
    }
    /**
     * 开始作业,状态变为2,即正在出库
     * @param billId
     * @return
     */
    @RequestMapping("/startAssignment")
    @ResponseBody
    public AjaxResult startAssignment(Integer billId){
        return toAjax(billOutWorkService.startAssignment(billId));
    }


    /**
     * 取消作业,状态变为1,即已审核状态
     * @param billId
     * @return
     */
    @RequestMapping("/endAssignment")
    @ResponseBody
    public AjaxResult endAssignment(Integer billId){
        return toAjax(billOutWorkService.endAssignment(billId));
    }
    /***
     *确认发货
     * @author 李帅辰
     * @date 2020/10/22 14:15
     * @return com.deer.wms.common.core.domain.AjaxResult
     */
    @RequestMapping("/editBillOut")
    @ResponseBody
    public AjaxResult editBillOut(Integer billId){
        return toAjax(billOutWorkService.editBillOut(billId));
    }
    /**
     * 出库回执
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("/Receipt")
    @Transactional(rollbackFor = Exception.class)
    public Result Receipt(@RequestBody InserData inserData) {
        List<BillOutDetailDto> billOutDetailList = inserData.getBillOutDetailList();//手持机扫描传来出库明细：有boxCode quantity itemCode
        BillOutMaster billOutMaster = inserData.getBillOutMaster();//手持机传来的出库单，有bill_no
        BoxInfo boxInfo = new BoxInfo();
        for (BillOutDetail billOutDetail : billOutDetailList) {
            //根据boxCode 查询任务列表的全部信息
            TaskInfoDto taskInfo = taskInfoService.selectTaskInfoByBoxCode(billOutDetail.getBoxCode());
//            if (taskInfo.getQuantity()==billOutDetail.getQuantity()&&taskInfo.getItemCode().equals(billOutDetail.getItemCode().trim())){
//                    //修改任务状态
//                    TaskInfo taskInfo1=new TaskInfo();
//                    taskInfo1.setId(taskInfo.getId());
//                    taskInfo1.setReceipt(1);
//                    taskInfoService.updateTaskInfo(taskInfo1);
//                    //将回执成功的出库的库存挪到流水表，然后删除box_item的库存
//                    BoxItemDto boxItemByBoxCode = boxItemService.billOutGetBoxItemByBoxCode(billOutDetail.getBoxCode());
//                    Long id = boxItemByBoxCode.getId();
//                    boxItemRecordService.insertBoxItemRecord(boxItemByBoxCode);
//                    boxItemService.deleteBoxItemById(id);
//                    //更新托盘状态
//                    boxInfo.setBoxCode(billOutDetail.getBoxCode());
//                    boxInfo.setBoxState(0);//0-不在货位 1-货位上  2-任务中
//                    boxInfo.setHasGoods(0);//   0-无货 1-有货
//                    boxInfo.setBoxCellId(0);//货位Id
//                    boxInfoService.updateBoxInfo(boxInfo);
//                    //更新货位信息
//                    CellInfo cellInfo = new CellInfo();
//                    cellInfo.setState(0);//状态  ：0-无托盘；1-有货；2-出库中；3-入库中；4故障
//                    cellInfo.setCellId(taskInfo.getCellId());
//                    cellInfoService.updateCellInfo(cellInfo);
//            }else {
//                return ResultGenerator.getout();
//            }
               //判断全部货物回执完毕，同步到出库记录表
                String billNo = billOutMaster.getBillNo();
                List<BillOutDetailDto> listByBillNo = billOutDetailService.findListByBillNo(billNo);
                for (BillOutDetailDto billOutDetailDto1 : listByBillNo) {
                    TaskInfoCriteria criteria = new TaskInfoCriteria();
                    //criteria.setBillId(billOutDetailDto1.getBillId());
                    int billOutDetasilCount = taskInfoService.getCountByBillIdAndReceipt(criteria);//获得该出库单下的全部任务的数量
                    criteria.setReceipt(1);//回执成功字段
                    int ReceirtCount = taskInfoService.getCountByBillIdAndReceipt(criteria);//获得该出库单下的全部任务已回执成功的数量
                    if(billOutDetasilCount==ReceirtCount){
                        for (BillOutDetailDto billOutDetailDto : listByBillNo) {
                            //更新出库单的状态
                            billOutMaster.setState(3);//任务完成的状态
                            billOutMasterService.updateBillOutMaster(billOutMaster);
                            //增加pickTask数据
                            PickTaskDto pickTask = new PickTaskDto();
                            pickTask.setCreateTime(billOutDetailDto.getCreateTime());//出库时间
                            pickTask.setBillOutDetailId(billOutDetailDto.getBillOutDetailId());
                            //pickTask.setWareId(billOutDetailDto.getWareId());//仓库
                            //pickTask.setPickQuantity(billOutDetailDto.getQuantity());//出库数量
                            pickTask.setPickState(1);//1-待下发，2-已下发任务,3-点数任务已完成，4-库存已扣减，5-取消
                            pickTask.setBatch(billOutDetailDto.getBatch()); //批次
                            pickTask.setBoxCode(billOutDetailDto.getBoxCode());//箱号
                            pickTaskService.savePickTask(pickTask);
                        }
//                        //同步历史表
//                        List<TaskInfo> taskInfoList = taskInfoService.selectTaskInfoByBillOutMasterId(billOutDetailDto1.getBillId());
//                        for (TaskInfo info : taskInfoList) {
//                            taskInfoHistoryService.saveTaskInfoHistory(info);
//                            //删除taskInfo
//                            taskInfoService.deleteById(info.getId());
//                            //taskInfoService.deleteById(info.getId());
//                        }
                    }

                }
        }
        return ResultGenerator.genSuccessResult();
    }
}
